package srdb.migration.Util;

import srdb.migration.MigrationToolkit;

import java.util.Arrays;

/**
 * Created by mervyn on 15-9-10.
 */
public class MigrationOperation implements IMigrationOperation {
    private Migration migration;

    public MigrationOperation(Migration migration) {
        if (migration == null)
            throw new IllegalArgumentException("migraion can not be null");
        this.migration = migration;
    }

    /**
     * 执行迁移操作
     */
    @Override
    public void execute() {
        MigrationToolkit.executeMigration(genParams(migration), migration.getSrcDBInfo(), migration.getDstDBInfo(), migration.getIuiOperation());
    }

    /**
     * 通过Migration类生成迁移执行参数
     *
     * @param migration
     * @return executeMigration方法所需参数
     */
    private String[] genParams(Migration migration) {
        if (migration == null)
            throw new IllegalArgumentException("arguments can not be null");
        if (migration.getSrcDBInfo() == null)
            throw new IllegalArgumentException("srcDBInfo can not be null");
        if (migration.getDstDBInfo() == null)
            throw new IllegalArgumentException("dstDBInfo can not be null");
        if (migration.getSchema() == null)
            throw new IllegalArgumentException("schema can not be null");
        StringBuilder arguments = new StringBuilder();

        if (migration.getMigMode() == Constants.MIG_DATA_ONLY) {
            if (migration.getTables() == null) {
                throw new IllegalArgumentException("tables can not be null");
            }
            if (migration.getCpBatchSize() > 0) {
                arguments.append("-cpBatchSize ").append(migration.getCpBatchSize());
            }
            if (migration.getBatchSize() > 1 && migration.getBatchSize() < 5000) {
                arguments.append(" -batchSize ").append(migration.getBatchSize());
            }
            if (migration.getFetchSize() > 0) {
                arguments.append(" -fetchSize ").append(migration.getFetchSize());
            }
            if (migration.getRetryCount() > 0) {
                arguments.append(" -retryCount ").append(migration.getRetryCount());
            }
            if (migration.getLoaderCount() > 0) {
                arguments.append(" -loaderCount ").append(migration.getLoaderCount());
            }
            arguments.append(" -dataOnly");
            if (migration.getTruncLoad()) {
                arguments.append(" -truncLoad");
            }
            String[] tables = migration.getTables();
            String tempStr = arraysToString(tables);
            arguments.append(" -tables ").append(tempStr);

        } else {
            arguments.append("-dropSchema ").append(migration.getDropSchema());
            if (migration.getCpBatchSize() > 0) {
                arguments.append(" -cpBatchSize ").append(migration.getCpBatchSize());
            }
            if (migration.getBatchSize() > 1 && migration.getBatchSize() < 5000) {
                arguments.append(" -batchSize ").append(migration.getBatchSize());
            }
            if (migration.getFetchSize() > 0) {
                arguments.append(" -fetchSize ").append(migration.getFetchSize());
            }
            if (migration.getRetryCount() > 0) {
                arguments.append(" -retryCount ").append(migration.getRetryCount());
            }
            if (migration.getLoaderCount() > 0) {
                arguments.append(" -loaderCount ").append(migration.getLoaderCount());
            }

            if (migration.getMigMode() == Constants.MIG_ALL) {
            } else if (migration.getMigMode() == Constants.MIG_PARSE_ONLY) {
                arguments.append(" -parseOnly");
            } else if (migration.getMigMode() == Constants.MIG_SCHEMA_ONLY) {
                if (migration.getTables() == null) {
                    throw new IllegalArgumentException("tables can not be null");
                }
                String[] tables = migration.getTables();
                String tempStr = arraysToString(tables);
                arguments.append(" -schemaOnly -tables ").append(tempStr);
            } else {
                throw new IllegalArgumentException("migmode=" + migration.getMigMode() + " is not legal");
            }
        }

        arguments.append(" ").append(migration.getSchema());
        String[] params = arguments.toString().split(" ");
        System.out.println(arguments.toString());
        return params;
    }

    private String arraysToString(String[] tables) {
        if (tables == null || tables.length == 0)
            throw new IllegalArgumentException("tables can not be null");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tables.length; i++) {
            sb.append(tables[i]).append(",");
        }
        String tableStr = sb.toString();
        return tableStr.substring(0, tableStr.length() - 1);
    }
}
